package com.liangtian.mdm.system.group.controller;

import com.alibaba.fastjson.JSONObject;
import com.liangtian.mdm.base.utils.ResultObject;
import com.liangtian.mdm.util.RequestUtils;
import com.liangtian.mdm.system.group.model.SysGroup;
import com.liangtian.mdm.system.group.service.SysGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/sysGroup")
public class SysGroupController {

    @Autowired
    SysGroupService sysGroupService;

    /**
     * 右侧列表查询
     *
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/querySysGroup", method = RequestMethod.POST)
    public String queryList(HttpServletRequest request, SysGroup sysGroup) {
        Map<String, Object> param = RequestUtils.paramToMap(request);
        String surveyType = (String) param.get("survey_type");
        int start = surveyType.indexOf("_");
        surveyType = surveyType.substring(start + 1, surveyType.length());
        param.put("modelId", surveyType);
        Map<String, Object> resMap = sysGroupService.queryGroupBySurveyType(param);
        return JSONObject.toJSONString(resMap);
    }

    /**
     * 保存
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultObject saveGroupPer(HttpServletRequest request, String[] groupIds) {
        ResultObject result = null;
        String source = request.getParameter("isUsed");
        sysGroupService.save(request.getParameter("surveyType"), request.getParameter("userId"), request.getParameter("uGroupId"), groupIds, source);
        result = new ResultObject(1, "保存成功", "保存失败");
        return result;
    }
}
